package com.cole.miniapp.project.service.impl;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.cole.miniapp.project.domain.FcmUser;
import com.cole.miniapp.project.domain.FcmUserConsumption;
import com.cole.miniapp.project.domain.FcmUserFraction;
import com.cole.miniapp.project.enumeration.user.UserActivationEmun;
import com.cole.miniapp.project.enumeration.user.UserIdentityEmun;
import com.cole.miniapp.project.enumeration.user.UserLoginEmun;
import com.cole.miniapp.project.mapper.FcmUserConsumptionMapper;
import com.cole.miniapp.project.mapper.FcmUserFractionMapper;
import com.cole.miniapp.project.mapper.FcmUserMapper;
import com.cole.miniapp.project.service.UserService;
import com.cole.miniapp.project.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author: Ceanie
 * @Despriction:
 * @Date: Created in 21:27 2019-05-16
 * @Modify By:
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private FcmUserMapper fcmUserMapper;

    @Autowired
    private FcmUserConsumptionMapper fcmUserConsumptionMapper;

    @Autowired
    private FcmUserFractionMapper fcmUserFractionMapper;

    @Value("${show.row}")
    private int showRow;


    @Override
    public FcmUser getUserById(Long id) {
        return fcmUserMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public FcmUser userLogin(WxMaJscode2SessionResult wxMaJscode2SessionResult) {
        FcmUser fcmUser = fcmUserMapper.selectUserByOpenid(wxMaJscode2SessionResult.getOpenid());

        //没有该用户
        if (fcmUser == null || fcmUser.toString() == "") {


            fcmUser = new FcmUser();
            fcmUser.setmOpenid(wxMaJscode2SessionResult.getOpenid());
            fcmUser.setUnionid(wxMaJscode2SessionResult.getUnionid());
            fcmUser.setMoney(new BigDecimal(0.00));
            fcmUser.setFraction((long) 0);
            fcmUser.setState(UserLoginEmun.CODE_0.getCode());
            fcmUser.setStatus(UserActivationEmun.CODE_0.getCode());
            fcmUser.setType(UserIdentityEmun.CODE_0.getCode());
            fcmUser.setCreateTime(new Date());

            fcmUserMapper.insert(fcmUser);
            fcmUser = fcmUserMapper.selectUserByOpenid(wxMaJscode2SessionResult.getOpenid());
        } else {
            //有该用户,判断用户是否有unionid
            if (fcmUser.getUnionid() == null || "".equals(fcmUser.getUnionid().trim())) {
                //如果没有，微信服务器获取到了unionid，那么更新用户数据
                if (wxMaJscode2SessionResult.getUnionid() != null) {
                    fcmUser.setUnionid(wxMaJscode2SessionResult.getUnionid());
                    Parameter2 parameter2 = new Parameter2();
                    parameter2.setVar1(fcmUser.getUnionid());
                    parameter2.setVar2(fcmUser.getId());
                    fcmUserMapper.updateUnionidById(parameter2);
                }
            }
        }
        return fcmUser;
    }

    @Override
    public FcmUser userInfo(FcmUser fcmUser, WxMaUserInfo wxMaUserInfo) {
        Parameter3 parameter3 = new Parameter3();
        parameter3.setVar1(wxMaUserInfo.getNickName());
        parameter3.setVar2(wxMaUserInfo.getAvatarUrl());
        parameter3.setVar3(fcmUser.getId());
        fcmUserMapper.updateUserInfoById(parameter3);
        return fcmUserMapper.selectByPrimaryKey(fcmUser.getId());
    }

    @Override
    public FcmUser userPhone(FcmUser fcmUser, WxMaPhoneNumberInfo wxMaPhoneNumberInfo) {
        fcmUser.setPhone(wxMaPhoneNumberInfo.getPhoneNumber());
        fcmUser.setStatus(UserActivationEmun.CODE_1.getCode());
        fcmUserMapper.updateUserPhoneById(fcmUser);
        return fcmUserMapper.selectByPrimaryKey(fcmUser.getId());
    }

    /**
     * 查询用户消费
     * @param userId
     * @param state
     * @param page
     * @return
     */
    @Override
    public List<FcmUserConsumption> queryUserSpend(Long userId, long state, int page) {
        List<FcmUserConsumption> list;
        if (state == -1) {
            Parameter3 parameter3 = new Parameter3();
            parameter3.setVar1(userId);
            parameter3.setVar2(page * showRow);
            parameter3.setVar3(showRow);
            list = fcmUserConsumptionMapper.selectConsumptionByUserId(parameter3);
        } else {
            Parameter4 parameter4 = new Parameter4();
            parameter4.setVar1(userId);
            parameter4.setVar2(state);
            parameter4.setVar3(page * showRow);
            parameter4.setVar4(showRow);
            list = fcmUserConsumptionMapper.selectConsumptionByUserIdAndState(parameter4);
        }
        return list;
    }

    /**
     * 用户积分明细
     */
    @Override
    public List<FcmUserFraction> userFractionDetail(Long userId, long state, int page) {
        List<FcmUserFraction> list;
        //显示全部收入+支出获得积分
        if (state == -1) {
            Parameter3 parameter3 = new Parameter3();
            parameter3.setVar1(userId);
            parameter3.setVar2(page * showRow);
            parameter3.setVar3(showRow);
            list = fcmUserFractionMapper.selectFractionByUserId(parameter3);
        } else {
            //根据state显示收入或支出所获得的积分
            Parameter4 parameter4 = new Parameter4();
            parameter4.setVar1(userId);
            parameter4.setVar2(state);
            parameter4.setVar3(page * showRow);
            parameter4.setVar4(showRow);
            list = fcmUserFractionMapper.selectFractionByUserIdAndState(parameter4);
        }
        return list;
    }

}
